package by.amakarevich.services.impl;

import javax.inject.Inject;

import org.apache.commons.lang3.Validate;
import org.springframework.stereotype.Service;

import by.amakarevich.dataaccess.DiscountDao;
import by.amakarevich.datamodel.Discount;
import by.amakarevich.datamodel.Product;
import by.amakarevich.services.DiscountService;

@Service
public class DiscountServiceImpl implements DiscountService {
	
	@Inject
	private DiscountDao discountDao;

	@Override
	public Discount get(Long id) {
		Discount entity = discountDao.getById(id);
		return entity;
	}

	@Override
	public void createDiscount(Discount discount, Product product){
		Validate.isTrue(discount.getId() == null, "This method should be called for 'not saved yet' profile only. Use UPDATE instead");
		discount.setProduct(product);
		discountDao.insert(discount);
	}

	@Override
	public void deleteDiscount(Discount discount) {
		discountDao.delete(discount.getId());

	}
	
	@Override
	public void updateDiscount(Discount discount) {
		discountDao.update(discount);
	}

	@Override
	public void deleteAll() {
		discountDao.deleteAll();
	}
}
